using System;
using System.Drawing;
using System.Diagnostics;
using System.Collections;


namespace CORIDemo
//namespace GuavaLib
{
    public class PictureRegions
    {
        private FindingProcType proc;

        static string m_region;
        static int m_click_region;
        static string m_endregion;
        static Queue que;
        static int i;
        static string regn;
        //Egd location names

        public const string egdVocal = "Vocal Cords";

        public const string egdProx = "Proximal Esophagus";
        public const string egdMidEso = "Mid Esophagus";
        public const string egdDist = "Distal Esophagus";
        public const string egdCardis = "Cardia";
        public const string egdFundus = "Fundus";
        public const string egdBody = "Body";
        public const string egdAntrium = "Antrum";
        public const string egdPyloric = "Pyloric Sphincter";
        public const string egdDuoBulb = "Duodenal Bulb";
        public const string egdDuo2nd = "Duodenal 2nd Portion";
        public const string egdDuoApex = "Duodenum";
        public const string egdJejunum = "Jejunum";

        //COL & FLX location names
        public const string colRec = "Rectum";
        public const string colSig = "Sigmoid colon";
        public const string colDes = "Descending colon";
        public const string colSple = "Splenic flexure";
        public const string colTra = "Transverse colon";
        public const string colHep = "Hepatic flexure";
        public const string colAsc = "Ascending colon";
        public const string colIle = "Terminal ileum";
        public const string colCec = "Cecum";

        //ERCP names
        public const string ercpAcc = "Accessory pancreatic duct";
        public const string ercpBile = "Common bile duct";
        public const string ercpHep = "Common hepatic duct";
        public const string ercpCyst = "Cystic duct";
        public const string ercpDuo = "Duodenum";
        public const string ercpGall = "Gallbladder";
        public const string ercpLeft = "Left hepatic duct";
        public const string ercpMaj = "Major duodenal papilla";
        public const string ercpMin = "Minor duodenal papilla";
        public const string ercpPanc = "Pancreas";
        public const string ercpPanD = "Pancreatic duct";
        public const string ercpRight = "Right hepatic duct";

        /**************************
         * Anastamosis site
                Anus
                Rectum
                Sigmoid colon
                Descending colon
                Splenic flexure
                Transverse colon
                Hepatic flexure
                Ascending colon
                Cecum
                Terminal ileum
                Ileum
        *************************/

        static Color[] colors =		// these are 4 bit Windows colors
		{
			//Color.FromArgb(255,   0,   0,   0),			// black - do not use
			Color.FromArgb(255, 128,   0,   0),			// dark red
			Color.FromArgb(255,   0, 128,   0),			// dark green
			Color.FromArgb(255, 128, 128,   0),			// dark green-yellow
			Color.FromArgb(255,   0,   0, 128),			// dark blue
			Color.FromArgb(255, 128,   0, 128),			// purple
			Color.FromArgb(255,   0, 128, 128),			// dark green-blue
			Color.FromArgb(255, 192, 192, 192),			// light gray
			Color.FromArgb(255, 128, 128, 128),			// dark gray
			Color.FromArgb(255, 255,   0,   0),			// bright red
			Color.FromArgb(255,   0, 255,   0),			// bright green
			Color.FromArgb(255, 255, 255,   0),			// bright yellow
			Color.FromArgb(255,   0,   0, 255),			// bright blue
			Color.FromArgb(255, 255,   0, 255),			// lavender
			Color.FromArgb(255,   0, 255, 255),			// cyan
			//Color.FromArgb(255, 255, 255, 255)			// white - do not use
		};

        static public string DefaultPart { get { return (m_region == null) ? "" : m_region; } }
        static public string EndPart { get { return (m_endregion == null) ? "" : m_endregion; } }


        public int ClickRegion
        {
            set
            {
                int v = value;
                switch (v)
                {
                    default: m_click_region = 0; break;
                    case 0x0001: m_click_region = 1; break;
                    case 0x0002: m_click_region = 2; break;
                    case 0x0004: m_click_region = 3; break;
                    case 0x0008: m_click_region = 4; break;
                    case 0x0010: m_click_region = 5; break;
                    case 0x0020: m_click_region = 6; break;
                    case 0x0040: m_click_region = 7; break;
                    case 0x0080: m_click_region = 8; break;
                    case 0x0100: m_click_region = 9; break;
                    case 0x0200: m_click_region = 10; break;
                    case 0x0400: m_click_region = 11; break;
                    case 0x0800: m_click_region = 12; break;
                    case 0x1000: m_click_region = 13; break;
                    case 0x2000: m_click_region = 14; break;
                    case 0x4000: m_click_region = 15; break;
                    case 0x8000: m_click_region = 16; break;
                }
                switch (proc)
                {
                    case FindingProcType.COL:
                        setColRegionName();
                        break;

                    case FindingProcType.EGD:
                        setEgdRegionName();
                        break;

                    case FindingProcType.ERCP:
                        setErcpRegionName();
                        break;

                    case FindingProcType.FLX:
                        setColRegionName();
                        break;

                    default:
                        m_region = "-none-";
                        break;
                }
            }
        }


        public PictureRegions(FindingProcType p)
        {
            proc = p;
            que = new Queue();
            i = 0;
        }

        private void setColRegionName()
        {
            switch (m_click_region)
            {
                case 1: regn = colRec; break;
                case 2: regn = colSig; break;
                case 3: regn = colDes; break;
                case 4: regn = colSple; break;
                case 5: regn = colTra; break;
                case 6: regn = colHep; break;
                case 7: regn = colAsc; break;
                case 8: regn = colIle; break;
                case 9: regn = colCec; break;
                /*
                case 10:	m_region = "";		break;
                case 11:	m_region = "";		break;
                case 12:	m_region = "";		break;
                case 13:	m_region = "";		break;
                case 14:	m_region = "";		break;
                case 15:	m_region = "";		break;
                case 16:	m_region = "";		break;
                */
                default: regn = ""; break;
            }
            //Trace.WriteLine(m_region, m_click_region.ToString());
            que.Enqueue(regn);
            setRegions();
        }

        /*
Antrum
Body
Cardia
Distal Esophagus
Duodenal 2nd Portion
Duodenal Apex
Duodenal Bulb
Fundus
Jejunum
Mid Esophagus
Proximal esophagus
Pyloric Spincter
Vocal Cords

"Antrum",
"Body",
"Cardia",
"Distal Esophagus",
"Duodenal 2nd Portion",
"Duodenal Apex",
"Duodenal Bulb",
"Fundus",
"Jejunum",
"Mid Esophagus",
"Proximal Esophagus",
"Pyloric Spincter",
"Vocal Cords"});

         */

        private void setEgdRegionName()
        {
            switch (m_click_region)
            {
                case 1: regn = egdProx; break;
                case 2: regn = egdMidEso; break;
                case 3: regn = egdDist; break;
                case 4: regn = egdCardis; break;
                case 5: regn = egdFundus; break;
                case 6: regn = egdBody; break;
                case 7: regn = egdAntrium; break;
                case 8: regn = egdPyloric; break;
                case 9: regn = egdDuoBulb; break;
                case 10: regn = egdDuo2nd; break;
                case 11: regn = egdDuoApex; break;
                case 12: regn = egdJejunum; break;
                case 13: regn = ""; break;
                case 14: regn = ""; break;
                case 15: regn = ""; break;
                case 16: regn = ""; break;
                default: regn = ""; break;
            }
            que.Enqueue(regn);
            setRegions();
        }

        private void setErcpRegionName()
        {
            switch (m_click_region)
            {
                case 1: regn = ercpRight; break;
                case 2: regn = ercpLeft; break;
                case 3: regn = ercpHep; break;
                case 4: regn = ercpCyst; break;
                case 5: regn = ercpGall; break;
                case 6: regn = ercpBile; break;
                case 7: regn = ""; break;
                case 8: regn = ""; break;
                case 9: regn = ercpPanc; break;
                case 10: regn = ercpPanD; break;
                case 11: regn = ercpDuo; break;
                case 12: regn = ercpMaj; break;
                case 13: regn = ercpMin; break;
                case 14: regn = ercpAcc; break;
                case 15: regn = ""; break;
                case 16: regn = ""; break;
                default: regn = ""; break;
            }
            que.Enqueue(regn);
            setRegions();
        }

        public int GetRegionId(Color c)
        {
            int j = 0;
            for (j = 0; j < colors.Length; j++)
                if (colors[j] == c)
                    break;
            if (j == colors.Length)
                return 0;
            return 1 << j;
        }

        public static void setComboEgdRegion(System.Windows.Forms.ComboBox cb)
        {
            cb.Items.Clear();
            cb.Items.Add(egdProx);
            cb.Items.Add(egdMidEso);
            cb.Items.Add(egdDist);
            cb.Items.Add(egdCardis);
            cb.Items.Add(egdFundus);
            cb.Items.Add(egdBody);
            cb.Items.Add(egdAntrium);
            cb.Items.Add(egdPyloric);
            cb.Items.Add(egdDuoBulb);
            cb.Items.Add(egdDuo2nd);
            cb.Items.Add(egdDuoApex);
            cb.Items.Add(egdJejunum);
        }

        public static void setComboColRegion(System.Windows.Forms.ComboBox cb)
        {
            cb.Items.Clear();
            cb.Items.Add(colRec);
            cb.Items.Add(colSig);
            cb.Items.Add(colDes);
            cb.Items.Add(colSple);
            cb.Items.Add(colTra);
            cb.Items.Add(colHep);
            cb.Items.Add(colAsc);
            cb.Items.Add(colIle);
            cb.Items.Add(colCec);
        }

        public static void setComboFlxRegion(System.Windows.Forms.ComboBox cb)
        {
            cb.Items.Clear();
            cb.Items.Add(colRec);
            cb.Items.Add(colSig);
            cb.Items.Add(colDes);
            cb.Items.Add(colSple);
            cb.Items.Add(colTra);
            //cb.Items.Add(colHep);
            //cb.Items.Add(colAsc);
            //cb.Items.Add(colIle);
            //cb.Items.Add(colCec);
        }

        public static void setComboErcpRegion(System.Windows.Forms.ComboBox cb)
        {
            cb.Items.Clear();
            cb.Items.Add(ercpAcc);
            cb.Items.Add(ercpBile);
            cb.Items.Add(ercpHep);
            cb.Items.Add(ercpCyst);
            cb.Items.Add(ercpDuo);
            cb.Items.Add(ercpGall);
            cb.Items.Add(ercpLeft);
            cb.Items.Add(ercpMaj);
            cb.Items.Add(ercpMin);
            cb.Items.Add(ercpPanc);
            cb.Items.Add(ercpPanD);
            cb.Items.Add(ercpRight);
        }

        private static void setRegions()
        {
            if (que.Count == 0)
                return;
            foreach (string s in que)
            {
                i += 1;
                if (i == 1)
                    m_region = s;
                else if (i == 2)
                {
                    m_endregion = s;
                    i = 0;
                }
                else
                    return;
            }
            que.Clear();
        }
    }
}
